﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Input;

namespace BoxApp1
{
    public class Map
    {

        public static int MaxRow { get; set; }
        public static int MaxCol { get; set; }
        public static int PlayerRow { get; set; }
        public static int PlayerCol { get; set; }
        public static Tile[,] GridMatrix { get; set; }
        public static Tile[,] OriginalGridMatrix { get; set; }
        public static int CurrentLevel { get; set; }

        

        public static bool IsMoveable(int row, int col)
        {
            if (GridMatrix[2 * PlayerRow - row, 2 * PlayerCol - col].TileType == "B" && GridMatrix[3 * PlayerRow - 2 * row, 3 * PlayerCol - 2 * col].IsWalkable)
            {
                return true;
            }
            return false;
        }

        public static void MoveBox(int row, int col)
        {
            if (OriginalGridMatrix[2 * PlayerRow - row, 2 * PlayerCol - col].TileType == "G")
            {
                GridMatrix[2 * PlayerRow - row, 2 * PlayerCol - col].ChangeTile("G");
            }
            else
            {
                GridMatrix[2 * PlayerRow - row, 2 * PlayerCol - col].ChangeTile("E");
            }
            GridMatrix[3 * PlayerRow - 2 * row, 3 * PlayerCol - 2 * col].ChangeTile("B");
        }

        public static bool DidWeWin()
        {
            for (int row = 0; row < MaxRow; row++)
            {
                for (int col = 0; col < MaxCol; col++)
                {
                    if (GridMatrix[row, col].TileType == "G") return false;
                }
            }
            return true;
        }

        public static void ReadMapFile()
        {
            string line = null;
            try
            {
                string file = Path.GetFullPath("Resources/Maps/" + CurrentLevel + ".txt");
                using (StreamReader sr = new StreamReader(file))
                {
                    line = sr.ReadToEnd();
                }
            }
            catch
            {
                MessageBox.Show("Hiba! Okok: elérési útvonal rossz v nincs több pálya vagy Build actiont: Content-re állítni, Copy always!");
                Application.Current.Shutdown();
            }
            MaxRow = line.Split('\n').Length;
            MaxCol = 0;
            for (int i = 0; i < line.Length; i++)
            {
                if (line[i] != Environment.NewLine[0]) { MaxCol++; } else break;
            }
            GridMatrix = new Tile[MaxRow, MaxCol];
            OriginalGridMatrix = new Tile[MaxRow, MaxCol];
            for (int rows = 0; rows < MaxRow; rows++)
            {
                for (int cols = 0; cols < MaxCol; cols++)
                {
                    //mátrixba pakolgatás
                    GridMatrix[rows, cols] = new Tile(line[rows * (MaxCol + Environment.NewLine.Length) + cols].ToString());
                    GridMatrix[rows, cols].TileType = line[rows * (MaxCol + Environment.NewLine.Length) + cols].ToString();
                    if (GridMatrix[rows, cols].TileType == "P") { PlayerRow = rows; PlayerCol = cols; GridMatrix[rows, cols].TileType = " "; }
                    //másolat 
                    OriginalGridMatrix[rows, cols] = new Tile(line[rows * (MaxCol + Environment.NewLine.Length) + cols].ToString());
                    OriginalGridMatrix[rows, cols].TileType = line[rows * (MaxCol + Environment.NewLine.Length) + cols].ToString();
                    if (OriginalGridMatrix[rows, cols].TileType == "P") { PlayerRow = rows; PlayerCol = cols; OriginalGridMatrix[rows, cols].TileType = " "; }
                }
            }
        }

        public static void KeyPressed(KeyEventArgs e)
        {
            switch (e.Key)
            {
                case (Key.Up):
                    if (GridMatrix[PlayerRow - 1, PlayerCol].IsWalkable)
                    {
                        PlayerRow--;
                    }
                    else
                    {
                        if (IsMoveable(PlayerRow + 1, PlayerCol))
                        {
                            MoveBox(PlayerRow + 1, PlayerCol);
                            PlayerRow--;
                        }
                    }
                    break;
                case (Key.Down):
                    if (GridMatrix[PlayerRow + 1, PlayerCol].IsWalkable)
                    {
                        PlayerRow++;
                    }
                    else
                    {
                        if (IsMoveable(PlayerRow - 1, PlayerCol))
                        {
                            MoveBox(PlayerRow - 1, PlayerCol);
                            PlayerRow++;
                        }
                    }
                    break;
                case (Key.Left):
                    if (GridMatrix[PlayerRow, PlayerCol - 1].IsWalkable)
                    {
                        PlayerCol--;
                    }
                    else
                    {
                        if (IsMoveable(PlayerRow, PlayerCol + 1))
                        {
                            MoveBox(PlayerRow, PlayerCol + 1);
                            PlayerCol--;
                        }
                    }
                    break;
                case (Key.Right):
                    if (GridMatrix[PlayerRow, PlayerCol + 1].IsWalkable)
                    {
                        PlayerCol++;
                    }
                    else {
                        if (IsMoveable(PlayerRow, PlayerCol - 1))
                        {
                            MoveBox(PlayerRow, PlayerCol - 1);
                            PlayerCol++;
                        }
                    }
                    break;
                default: return;
            }
        }
    }
}
